<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use App\Model\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $role = Role::get();
        return view("admin.role.list",compact("role"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //获取提交的数据
        $input = $request->except("_token");
        $res = Role::create($input);
        //dd($res);

        if($res){
            $data = [
                'status' => 0,
                'message'=> '添加成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '添加失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $role = Role::find($id);
        return view("admin.role.edit",compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //1.根据id修改值
        $res = Role::where("id",$request->input("role_id"))->update($request->except("role_id"));

        if($res){
            $data = [
                'status' => 0,
                'message'=> '修改成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '修改失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $role = Role::find($id);
        $res = $role->delete();

        \DB::table("role_permission")->where("role_id",$id)->delete();
        if($res){
            $data = [
                'status' => 0,
                'message'=> '删除成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '删除失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    //角色多删除方法
    public function delAll(Request $request){
        $input = $request->input("ids");

        $res = Role::destroy($input);

        foreach ($input as $id){
            \DB::table("role_permission")->where("role_id",$id)->delete();
        }
        if($res){
            $data = [
                'status' => 0,
                'message'=> '删除成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '删除失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    //角色授权方法
    public function auth($id){
        //获取当前角色
        $role = Role::find($id);
        //获取所有权限列表
        $perms = Permission::get();

        //获取当前角色拥有的权限
        $own_perms = $role->permission;
        //dd($own_permission);
        //角色拥有的权限id
        $own_pers=[];
        foreach ($own_perms as $v){
            $own_pers[] = $v->id;
        }
        //dd($own_perms);

        return view('admin.role.auth',compact('role','perms','own_pers'));
    }

    //处理授权的方法
    public function doauth(Request $request){
        //获取权限id
        $input = $request->except("_token");
        //dd($input);

        //删除当前角色拥有的权限
        \DB::table("role_permission")->where("role_id",$input['role_id'])->delete();

        //添加新授予的权限
        if(!empty($input['permission_id'])){
            foreach ($input['permission_id'] as $item) {
                \DB::table("role_permission")->insert(['role_id'=>$input['role_id'],'permission_id'=>$item]);
            }
        }

        return redirect("admin/role");
    }
}
